package com.huoniu.api.exception;

import com.huoniu.commons.utils.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理类
 *
 * @author songhailong
 * @create 2017-10-20 19:07
 */
@ControllerAdvice
public class GlobleExceptionHandler {
	
	Logger logger = LoggerFactory.getLogger(getClass());
	
	/**
	 * 全局异常捕捉处理
	 *
	 * @param ex
	 * @return
	 */
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public ResultEntity defaultErrorHandler(Exception ex) throws Exception {
		logger.error(ex.getMessage(), ex);
		ResultEntity resultEntity = new ResultEntity();
		resultEntity.setResMsg(ex.getMessage());
		// 404
		if (ex instanceof org.springframework.web.servlet.NoHandlerFoundException) {
			resultEntity.setResCode(404);
		} else { // 500
			resultEntity.setResCode(500);
		}
		resultEntity.setStatus(false);
		resultEntity.setData(null);
		return resultEntity;
	}
}